<template>
  <el-dropdown class="user-avatar-wrapper" @command="handleCommand" trigger="click">
    <div class="avatar-box">
      <div v-if="userInfo.avatar" class="avatar-circle">
        <img :src="'/api' + userInfo.avatar" class="avatar-image-small">
      </div>
      <div v-else class="avatar-circle">
        <i class="el-icon-user"></i>
      </div>
      <span class="username">{{userInfo.username}}</span>
      <i class="el-icon-caret-bottom"></i>
    </div>

    <el-dropdown-menu slot="dropdown" class="user-dropdown">
      <el-dropdown-item command="userCenter">
        <i class="el-icon-user-solid dropdown-icon"></i>
        个人中心
      </el-dropdown-item>
      <el-dropdown-item command="loginOut">
        <i class="el-icon-switch-button dropdown-icon"></i>
        退出登录
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>

export default {
  inject: ['userInfo'],
  created() {
    console.log(this.userInfo)
  },
  name: 'UserAvatar',
  data() {
    return {
      imgBaseUrl: this.HOST,
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'userCenter') {
        this.$router.push({ path: '/personInfo' })
      }
      if (command === 'loginOut') {
        this.loginOut()
      }
    },
    loginOut() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem("backUser")
        this.$router.push({ path: '/login'});
      })
    }
  }
}
</script>

<style lang="less">
.user-avatar-wrapper {
  float: left;
  padding: 3px 0 3px 20px;
  margin-left: 20px;
  border-left: solid 1px rgba(0,0,0,0.05);
  cursor: pointer;
  transition: all 0.3s;

  .avatar-box {
    outline: none;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    border-radius: 30px;
    background-color: rgba(255, 250, 230, 0.6);
    transition: all 0.3s;

    &:hover {
      background-color: rgba(255, 250, 230, 1);
      box-shadow: 0 2px 8px rgba(255, 180, 0, 0.1);
    }

    .avatar-circle {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      background: linear-gradient(135deg, #FFA500, #FFD700);
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      box-shadow: 0 2px 6px rgba(255, 180, 0, 0.2);

      i {
        font-size: 18px;
      }

      .avatar-image-small {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 50%;
      }
    }
  }

  .username {
    font-size: 14px;
    color: #606266;
    font-weight: 500;
    transition: all 0.3s;
  }

  .el-icon-caret-bottom {
    font-size: 12px;
    color: #909399;
    transition: all 0.3s;
    margin-left: 4px;
  }

  &:hover {
    .username {
      color: #FFA500;
    }

    .el-icon-caret-bottom {
      transform: rotate(180deg);
      color: #FFA500;
    }
  }
}

.user-dropdown {
  margin-top: 10px;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border: none;
  overflow: hidden;

  .el-dropdown-menu__item {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    transition: all 0.3s;

    &:hover {
      background-color: #fffaf0;
      color: #FFA500;

      .dropdown-icon {
        color: #FFA500;
      }
    }
  }

  .dropdown-icon {
    margin-right: 10px;
    font-size: 16px;
    color: #909399;
  }
}
</style>